package com.lpit.csdn;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpClient {

    @Test
    public void testLinked() throws Exception {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建GET请求
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        httpGet.setHeader("use-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");

        // 获取响应结果
        CloseableHttpResponse response = httpClient.execute(httpGet);

        if (response.getStatusLine().getStatusCode() == 200) {
            String html = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.err.println(html);
        }

        httpClient.close();
        response.close();
    }

    @Test
    public void testJsoup() throws Exception {
        // 创建HttpClient
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建GET请求
        HttpGet httpGet = new HttpGet("https://www.cnblogs.com/sam-uncle/category/1469093.html");
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");

        // 获取响应
        CloseableHttpResponse response = httpClient.execute(httpGet);

        // 获取页面内容
        if (response.getStatusLine().getStatusCode() == 200) {
            String html = EntityUtils.toString(response.getEntity(), "UTF-8");

            // 创建Document对象
            Document document = Jsoup.parse(html);

            // 获取博客列表
            Element blog = document.getElementsByClass("entrylist").first();
            Elements blogList = blog.getElementsByClass("entrylistItem");
            for (Element element : blogList) {
                Elements title = element.select("a[class='entrylistItemTitle'] span");
                System.err.println(title.html());
            }
        }

        response.close();
        httpClient.close();
    }

    @Test
    public void testApi() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet("https://silkroad.csdn.net/api/v2/assemble/list/channel/search_hot_word?new_hot_flag=1&channel_name=pc_hot_word&size=20&user_name=weixin_43347659&platform=pc&imei=10_20960811560-1623721797026-245775");
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
        httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
        httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpGet.setHeader("Origin", "https://blog.csdn.net");
        httpGet.setHeader("Accept-Encoding", "gzip, deflate, br");

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);

            if (response.getStatusLine().getStatusCode() == 200) {
                System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            assert response != null;
            response.close();
        }

        httpClient.close();
    }

    @Test
    public void testCookie() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet("https://mall.csdn.net/myorder?spm=1001.2014.3001.5137");
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
        httpGet.setHeader("Cookie", "yourCookie");

        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("==============================开始打印页面==============================");
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("==============================结束打印页面==============================");
        }

        httpClient.close();
        response.close();
    }

    @Test
    public void testLogin() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost("https://passport.csdn.net/v1/register/pc/login/doLogin");
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
        httpPost.setHeader("Accept", "application/json, text/plain, */*");
        httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("Host", "passport.csdn.net");
        httpPost.setHeader("Origin", "https://passport.csdn.net");
        httpPost.setHeader("Referer", "https://passport.csdn.net/login?code=applets");

        // 配置登录参数
        List<NameValuePair> pairList = new ArrayList<NameValuePair>();
        pairList.add(new BasicNameValuePair("loginType", "1"));
        pairList.add(new BasicNameValuePair("pwdOrVerifyCode", "password"));
        pairList.add(new BasicNameValuePair("uaToken", ""));
        pairList.add(new BasicNameValuePair("userIdentification", "username"));
        pairList.add(new BasicNameValuePair("webUmidToken", ""));

        httpPost.setEntity(new UrlEncodedFormEntity(pairList, HTTP.UTF_8));

        CloseableHttpResponse response = httpClient.execute(httpPost);

        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("登录成功");
            // 这里要注销请求，否则会影响后续的请求
            httpPost.abort();

            HttpGet httpGet = new HttpGet("https://mall.csdn.net/myorder?spm=1001.2014.3001.5137");
            httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");

            CloseableHttpResponse response1 = httpClient.execute(httpGet);
            if (response1.getStatusLine().getStatusCode() == 200) {
                System.out.println("==============================开始打印页面==============================");
                System.out.println(EntityUtils.toString(response1.getEntity()));
                System.out.println("==============================结束打印页面==============================");
            }
            response1.close();
        }

        response.close();
        httpClient.close();
    }

}
